//
//  SocketEngine.h
//  Socket.IO-Client
//
//  Created by alanwang.dev on 16/8/8.
//  Copyright © 2016年 com.alanwang.dev. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "SocketEngineSpec.h"
#import "SocketEngineClient.h"
#import "SocketIOClient.h"
#import "SocketEngineWebSocket.h"
#import "SocketEnginePollable.h"
#import "WebSocket.h"
#import "SocketEngineClient.h"
#import "SocketTypes.h"

@interface URLSDict : NSObject
@property (nonatomic, strong) NSURL *firstURL;
@property (nonatomic, strong) NSURL *secondURL;
@end

// 多继承
@interface SocketEngine : SocketEngineSpec <SocketEnginePollableProtocol, SocketEngineWebSocketDelegate>

//@property (nonatomic, assign) dispatch_queue_t emitQueue;
//@property (nonatomic, assign) dispatch_queue_t handleQueue;
//@property (nonatomic, assign) dispatch_queue_t parseQueue;
//
//@property (nonatomic, strong) NSDictionary *connectParams;

//@property (nonatomic, strong) NSMutableArray<NSString *> *postWait;
//@property (nonatomic, assign) BOOL waitingForPoll;
//@property (nonatomic, assign) BOOL waitingForPost;

//@property (nonatomic, assign) BOOL closed;
//@property (nonatomic, assign) BOOL connected;
//@property (nonatomic, strong) NSMutableArray<NSHTTPCookie *> *cookies;
//@property (nonatomic, assign) BOOL doubleEncodeUTF8;
//@property (nonatomic, strong) NSDictionary *extraHeaders;
//@property (nonatomic, assign) BOOL fastUpgrade;
//@property (nonatomic, assign) BOOL forcePolling;
//@property (nonatomic, assign) BOOL forceWebsockets;
//@property (nonatomic, assign) BOOL invalidated;
//@property (nonatomic, assign) BOOL polling;
//@property (nonatomic, assign) BOOL probing;
@property (nonatomic, strong) NSURLSession *session;
//@property (nonatomic, copy) NSString *sid;
//@property (nonatomic, copy) NSString *socketPath;
@property (nonatomic, strong) NSURL *urlPolling;
@property (nonatomic, strong) NSURL *urlWebSocket;
//@property (nonatomic, assign) BOOL websocket;
//@property (nonatomic, strong) WebSocket *ws;
//
//@property (nonatomic, weak) id<SocketEngineClient> client;
@property (nonatomic, weak) id<NSURLSessionDelegate> sessionDelegate;
@property (nonatomic, copy) NSString *logType;
@property (nonatomic, strong) NSURL *url;
@property (nonatomic, assign) CGFloat pingInterval;
@property (nonatomic, assign) CGFloat pingTimeout;

@property (nonatomic, assign) NSInteger pongsMissed;
@property (nonatomic, assign) NSInteger pongsMissedMax;
@property (nonatomic, strong) ProbeWaitQueue *probeWait;
@property (nonatomic, assign) BOOL secure;
@property (nonatomic, assign) BOOL selfSigned;
@property (nonatomic, assign) BOOL voipEnabled;

- (instancetype)initWithClient:(id)client url:(NSURL *)url;
- (instancetype)initWithClient:(id)client url:(NSURL *)url options:(NSDictionary *)options;
- (void)connect;
- (void)didError:(NSString *)error;
- (void)disconnect:(NSString *)reason;
- (void)doFastUpgrade;

// We had packets waiting for send when we upgraded
// Send them raw
- (void)flushWaitingForPostToWebSocket;

- (void)parseEngineData:(NSData *)data;

- (void)parseEngineMessage:(NSString *)message fromPolling:(BOOL)fromPolling;

/// Write a message, independent of transport.
- (void)write:(NSString *)msg type:(SocketEnginePacketType)type Data:(NSArray<NSData *> *)data;

// Delegate methods
- (void)websocketDidConnect:(WebSocket *)socket;

- (void)websocketDidDisconnect:(WebSocket *)socket error:(NSError *)error;
@end
